<?php

class DataTable extends Block {

	public $data = array();
	public $titles=array();

	public function show(){
		$this->parseFromFile( __DIR__ ."/DataTable.tpl.std.html");
		
		//Encabezados
		if(is_array($this->titles) && !empty($this->titles)){
			if(isset($this->title)){
				$otitle="";
				$btitle=$this->title->block;
				
				foreach($this->titles as $key => $item){
					$this->title->item  = $item;
					$this->title->key   = $key;
					list($sort,$order)=isset($_GET['sort'])?explode(",",$_GET['sort']):array(0,0);
					if($sort==$key){
						$this->title->order=$order?"&#8896;":"&#8897;";
						$this->title->reorder=($order+1)%2;
					}else{
						$this->title->order="";
						$this->title->reorder=1;
					}
					
					$otitle.=$this->title->replace();
				}
				$this->title->block=$otitle;
			}
		
		}
		
		//Data
		if(is_array($this->data)){
			$orow=""; //variable temporal donde se acumulan los rows
			foreach($this->data as $rowId => $row){
				
				if(isset($this->row)){
					if(isset($this->row->cell)){
						$ocell="";//variable temporal donde se acumulan los cells
						$bcell=$this->row->cell->block; //salvo el bloque original, lo usaremos luego.
						foreach($row as $key => $item){
								
								$this->row->cell->item = $item;
								$ocell.=$this->row->cell->replace();
						}
						$this->row->cell->block=$ocell; //reemplazo el bloque y en su lugar pongo todas las celdas.
						
					}
					
					
					$orow.=$this->row->replace();
					$this->row->cell->block=$bcell;
				}
				
			}
			$this->row->block=$orow;
			$orow="";
			
			
			
		}
		
		
		return $this->replace();
		
	}

}

?>